
import Constants from '../../utils/Constants';
import { message } from "antd";
import { queryRegCustomers, getRegCustomer, deleteRegCustomer, updateRegCustomer, transferRegCustomer, sendFinance, queryFinanceRecords, queryInvoiceRecords, exportRegCustomers, getInvoiceRecords, deleteInvoiceRecords, updateInvoiceRecord, queryGoods } from "../../services/vip/vipManage";

const R = require('ramda');
export default {
    namespace: 'vipManage',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        fPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        invoPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        fList: [],
        goodsList: [],
        invoList: [],
        dataSource: {}
    },
    reducers: {
        setRegCustomerList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },

        setFinanceList(state, { payload }) {
            return { ...state, fList: payload.list, fPage: payload.page }
        },

        setInvoiceList(state, { payload }) {
            return { ...state, invoList: payload.list, invoPage: payload.page }
        },

        setGoodsList(state, { payload }) {
            return { ...state, goodsList: payload.goodsList }
        },

        setRegCustomer(state, { payload }) {
            return { ...state, dataSource: payload.dataSource }
        },

        setInvoice(state, { payload }) {
            return { ...state, dataSource: payload.dataSource }
        }
    },
    effects: {
        /**
         * 分页查询注册会员客户
         */
        *queryRegCustomers({ payload }, { call, put, select }) {
            let page = yield select(state => state.vipManage.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;
            const res = yield call(queryRegCustomers, {
                cstName: R.isNil(payload) || R.isNil(payload.cstName) ? "" : payload.cstName,
                cstType: R.isNil(payload) || R.isNil(payload.cstType) ? "" : payload.cstType,
                vipType: R.isNil(payload) || R.isNil(payload.vipType) ? "" : payload.vipType,
                status: R.isNil(payload) || R.isNil(payload.status) ? "" : payload.status,
                page: page
            })
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setRegCustomerList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 分页查询财税发送记录
         */
        *queryFinanceRecords({ payload }, { call, put, select }) {
            let page = yield select(state => state.vipManage.fPage);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;
            const res = yield call(queryFinanceRecords, {
                cstName: R.isNil(payload) || R.isNil(payload.cstName) ? "" : payload.cstName,
                page: page
            })
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setFinanceList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 分页查询开票信息
         */
        *queryInvoiceRecords({ payload }, { call, put, select }) {
            let page = yield select(state => state.vipManage.invoPage);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;
            const res = yield call(queryInvoiceRecords, {
                cstName: R.isNil(payload) || R.isNil(payload.cstName) ? "" : payload.cstName,
                page: page
            })
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setInvoiceList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 根据id获取开票信息详情
         */
        *getInvoiceRecordById({ payload }, { call, put, select }) {
            const res = yield call(getInvoiceRecords, { id: payload.id });
            if (res.data.resultCode === "ok") {
                let invoice = res.data.content;
                yield put({
                    type: "setInvoice",
                    payload: { dataSource: invoice }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 删除开票信息
         */
        *deleteInvoiceRecords({ payload }, { call, put, select }) {
            const res = yield call(deleteInvoiceRecords, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");
                yield put({
                    type: "queryInvoiceRecords",
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 更新开票信息
         */
        *updateInvoiceRecord({ payload }, { call, put, select }) {
            const dataSource = payload.dataSource;
            const res = yield call(updateInvoiceRecord, { invoiceInfo: dataSource });
            if (res.data.resultCode === "ok") {
                message.info("开票信息更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },


        /**
         * 根据id获取会员客户详情
         */
        *getRegCustomerById({ payload }, { call, put, select }) {
            const res = yield call(getRegCustomer, { id: payload.id });
            if (res.data.resultCode === "ok") {
                let customer = res.data.content;
                yield put({
                    type: "setRegCustomer",
                    payload: { dataSource: customer }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 删除会员客户
         */
        *deleteRegCustomerById({ payload }, { call, put, select }) {
            const res = yield call(deleteRegCustomer, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");
                yield put({
                    type: "queryRegCustomers",
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新会员客户
         */
        *updateRegCustomer({ payload }, { call, put, select }) {
            const dataSource = payload.dataSource;
            const res = yield call(updateRegCustomer, { regCustomer: dataSource });
            if (res.data.resultCode === "ok") {
                message.info("会员客户信息更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 转移会员客户
         */
        *transferRegCustomerById({ payload }, { call, put, select }) {
            const res = yield call(transferRegCustomer, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("客户转移成功！");
                yield put({
                    type: "queryRegCustomers",
                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 发送财税附件邮件
         */
        *sendFinanceEmail({ payload }, { call, put, select }) {
            const res = yield call(sendFinance, { sendInfo: payload.sendInfo });
            if (res.data.resultCode === "ok") {
                message.info("财税附件邮件发送成功！");
                if (payload.onSuccess) payload.onSuccess();
                /*                 yield put({
                                    type: "queryRegCustomers",
                                    payload: R.isNil(payload.searchConditions) ? {} : payload.searchConditions
                                }); */
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 导出注册会员客户
         */
        *exportRegCustomers({ payload }, { call, put, select }) {
            const res = yield call(exportRegCustomers, {
                cstName: R.isNil(payload) || R.isNil(payload.cstName) ? "" : payload.cstName,
                cstType: R.isNil(payload) || R.isNil(payload.cstType) ? "" : payload.cstType,
                vipType: R.isNil(payload) || R.isNil(payload.vipType) ? "" : payload.vipType,
                status: R.isNil(payload) || R.isNil(payload.status) ? "" : payload.status,
            })
        },

        /**
         * 查询产品列表
         */
        *queryGoods({ payload }, { call, put, select }) {
            const res = yield call(queryGoods)
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setGoodsList',
                    payload: {
                        goodsList: R.isNil(content) ? [] : content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    },
};